# -*- coding: UTF-8 -*-

from services.base import Core

class History:
    def __init__(self, filename):
        self.filename = filename
        
    def append(self, filename, image, ad, thumb, name):
        file = open(self.filename, "a")
        file.write("%s;%s;%s;%s;%s\n" % (filename, image, ad, thumb, name))
        file.close()
        return True
    
    def getall(self, n=0):
        file = open(self.filename)
        lines = file.readlines()
        if n>0:
            lines = lines[-n:]
        file.close()
        result = []
        for string in map(lambda s:s.strip(), lines):
            obj = Core()
            obj.parse(string)
            result.append(obj)
        return result
        
    def get(self, n):
        file = open(self.filename)
        lines = file.readlines()
        file.close()
        obj = Core()
        obj.parse(lines[n].strip())
        return obj
        
    def remove(self, n):
        file = open(self.filename)
        lines = file.readlines()
        file.close()
        del lines[n]
        file = open(self.filename, "w")
        file.write("".join(lines))
        file.close()
        
    def clear(self):
        file = open(self.filename, "w")
        file.close()
